import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
import '../theme/index.css'
import './assets/css/icon.css';
import 'babel-polyfill';
import axios from 'axios';
import ddapi from "./api/ddapi"
Vue.prototype.$ddapi = ddapi
import publicJS from "./components/public";
Vue.prototype.$publicJS = publicJS;
import VueCropper from 'vue-cropper'
Vue.use(VueCropper)
import * as dd from 'dingtalk-jsapi';
import {
    auth_login,
    auth_getJsConfig
} from '../src/api/index';
import {
    getUrl
} from '@/utils/index'
import moment from "moment"
Vue.prototype.$moment = moment



Vue.prototype.$http = axios;
Vue.config.productionTip = false;
Vue.use(ElementUI, {
    size: 'small'
});
const corpId = getUrl('corpId');
const agentId = getUrl('appKey')
sessionStorage.setItem('corpId', corpId)
sessionStorage.setItem('agentId', agentId)
let datas = {
    corpId: corpId,
    agentId: agentId
}
auth_getJsConfig(datas).then(res => {
  console.log('%c [ res ]-43', 'font-size:13px; background:pink; color:#bf2c9f;', res)
    if (res.status == 200) {
        var jsapilist = [
            'biz.contact.complexPicker',
            'biz.contact.choose' //选择人员
        ] //需要使用的jsapi列表
        //1、鉴权
        dd.config({
            agentid: res.data.data.agentId - 0,
            corpId: res.data.data.corpId, //必填，企业ID
            timeStamp: res.data.data.timeStamp, // 必填，生成签名的时间戳
            nonceStr: res.data.data.nonceStr, // 必填，生成签名的随机串
            signature: res.data.data.signature, // 必填，签名
            jsApiList: jsapilist // 必填，需要使用的jsapi列表，注意：不要带dd。
        });

        dd.error(function (err) { //验证失败
            console.log("进入到error中");
            console.log('dd error: ' + JSON.stringify(err));
        })

    }
    dd.ready(function () {
        dd.runtime.permission.requestAuthCode({
            corpId: corpId,
            onSuccess: function (result) {
                const data = {
                    code: result.code,
                    corpId: corpId,
                    agentId: agentId
                };
                auth_login(data)
                    .then(res => {
                        console.log(res, "gtttttt")
                        if (res.status == 200) {
                            sessionStorage.setItem('userId', res.data.data.userid)
                            sessionStorage.setItem('token', res.data.data.token)
                            sessionStorage.setItem('isAdmin', res.data.data.isAdmin)
                            sessionStorage.setItem('isSysAdmin', res.data.data.isSysAdmin)
                            sessionStorage.setItem('roleType', res.data.data.roleType)
                            sessionStorage.setItem('name', res.data.data.name)
                            sessionStorage.setItem('avatar', res.data.data.avatar)
                            axios.interceptors.request.use(
                                function (config) {
                                    config.headers.common['Authorization'] = res.data.data.token;
                                    return config;
                                },
                                function (err) {
                                    return Promise.reject(err);
                                }
                            );
                            
                            new Vue({
                                router,
                                render: h => h(App)
                            }).$mount('#app');
                        //   router.push('/home')
                        } else {
                            alert('登陆失败，请联系管理员');
                        }
                    })
                    .catch(err => {
                        console.log(err);
                    });
            },
            onFail: function (err) {
                alert(err);
            }
        });
    });
})